Learning Goals

At the end of this exercise, you will be able to:
1. Produce distribution maps in R.

Spatial Data in R

There are many packages and techniques for working with spatial data in R. We will cover just some of the basics. One nice package is ggmap, which allows us to get base maps from Google Maps, OpenStreetMap, and Stamen Maps. It also works well with ggplot2.

If you completed part 1 of today’s lab then you should have the following packages installed. If not, then you should get them going now. You need to do these in order!

devtools and ggmap

#install.packages("devtools")
library(devtools)
## Loading required package: usethis
#devtools::install_github("dkahle/ggmap")

Load the libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.1.8
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(here)
## here() starts at /Users/switters/Desktop/datascibiol
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test

Load the ggmap package

library(ggmap)
## ℹ Google's Terms of Service: <]8;;https://mapsplatform.google.comhttps://mapsplatform.google.com]8;;>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.

Let’s load our processed data from the first part of the lab.

spiders <- read_csv("data/spiders_with_locs.csv")%>% clean_names()
## Rows: 270 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): Family, Genus, Country, State, County, Locality, Collector
## dbl  (3): Accession, Latitude, Longitude
## date (1): Date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

There is an error in one of the coordinates that we will fix here.

spiders <- spiders %>% filter(latitude<=42)
glimpse(spiders)
## Rows: 269
## Columns: 11
## $ accession <dbl> 9038521, 9038522, 9038523, 9038524, 9038525, 9038526, 903852…
## $ family    <chr> "Telemidae", "Telemidae", "Telemidae", "Telemidae", "Telemid…
## $ genus     <chr> "Usofila", "Usofila", "Usofila", "Usofila", "Usofila", "Usof…
## $ country   <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA…
## $ state     <chr> "California", "California", "California", "California", "Cal…
## $ county    <chr> "Alameda", "Alameda", "Alameda", "Alameda", "Alameda", "Alam…
## $ locality  <chr> "Berkeley", "Castro Valley", "Niles, off Niles Cyn. on Palom…
## $ date      <date> 2019-03-02, 2019-03-24, 2019-01-02, 2019-02-18, 2019-01-25,…
## $ collector <chr> "LM Smith", "WM Pearce", "V Roth", "WG Bentinck", "R Schuste…
## $ latitude  <dbl> 37.87, 37.73, 37.60, 37.80, 37.80, 37.80, 37.80, 37.82, 37.8…
## $ longitude <dbl> -122.24, -122.07, -121.95, -122.16, -122.16, -122.16, -122.1…

Create Base Map

Our goal here is to plot the spiders locations from the columns which contain the latitude and longitude. First, we need to get a base map for plotting our points on. We could plot them without a base map, but that wouldn’t give us any context as to where they are in space. To get a base map we specify a min and max of each x and y coordinate, and create a bounding box.

We set the bounding box to a little outside our min and max locations with f = 0.05.

summary() gives us our min and max.

spiders %>% 
  select(latitude, longitude) %>% 
  summary()
##     latitude       longitude     
##  Min.   :34.67   Min.   :-124.1  
##  1st Qu.:37.88   1st Qu.:-122.5  
##  Median :38.19   Median :-122.1  
##  Mean   :38.47   Mean   :-121.6  
##  3rd Qu.:38.88   3rd Qu.:-120.5  
##  Max.   :41.80   Max.   :-115.5

Now we set the bounding box. We use the min and max values for latitude and longitude to set the range.

lat <- c(34.67, 41.80)
long <- c(-124.1, -115.5)
bbox <- make_bbox(long, lat, f = 0.05)

Let’s get a base map for our bounding box area. We will use the stamen maps because they are free. There are several different map types, including: terrain-labels, terrain-lines, toner, toner-2011, toner-background, toner-hybrid, toner-lines, toner-lite, and watercolor.

map1 <- get_map(bbox, maptype = "watercolor", source = "stamen")
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
ggmap(map1)

Adding Points to Base Map

ggmap works well with ggplot2. To add our points we only need to specify the x and y location similar to how we made charts in previous labs.

ggmap(map1) + 
  geom_point(data = spiders, aes(longitude, latitude)) +
  labs(x= "Longitude", y= "Latitude", title="Spider Locations")

Practice

Let’s practice with a data set that records sightings of grizzly bears (Ursos arctos) in Alaska data set.

grizzly <- read_csv("data/bear-sightings.csv")
## Rows: 494 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): bear.id, longitude, latitude
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  1. Use the range of the latitude and longitude to build an appropriate bounding box for your map.
grizzly %>% 
  select(longitude, latitude) %>% 
  summary()
##    longitude         latitude    
##  Min.   :-166.2   Min.   :55.02  
##  1st Qu.:-154.2   1st Qu.:58.13  
##  Median :-151.0   Median :60.97  
##  Mean   :-149.1   Mean   :61.41  
##  3rd Qu.:-145.6   3rd Qu.:64.13  
##  Max.   :-131.3   Max.   :70.37
lat <- c(55.02, 70.37)
long <- c(-131.3, -166.2)
bbox <- make_bbox(long, lat, f=0.5)
  1. Load a map from stamen in a terrain style projection and display the map.
map2 <- get_map(bbox, maptype = "terrain", source="stamen")
## ℹ Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
## Service Unavailable (HTTP 503). Failed to acquire tile
## /terrain/4/-1/2.png.Service Unavailable (HTTP 503). Failed to acquire tile
## /terrain/4/-1/3.png.Service Unavailable (HTTP 503). Failed to acquire tile
## /terrain/4/-1/4.png.Service Unavailable (HTTP 503). Failed to acquire tile
## /terrain/4/-1/5.png.
ggmap(map2)

  1. Build a final map that overlays the recorded observations of grizzly bears in Alaska.
ggmap(map2)+
  geom_point(data=grizzly, aes(x=longitude, y=latitude), size=0.4)+
  labs(x="Longitude", y="Latitude", title="Ursos arctos")

Wrap-up

Please review the learning goals and be sure to use the code here as a reference when completing the homework.

–>Home